package com.zcsy.manage.web.news;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.manage.model.base.CellInfo;
import com.zcsy.manage.model.news.CellManual;
import com.zcsy.manage.model.news.NoticeInfo;
import com.zcsy.manage.model.news.NoticeRead;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.service.common.CommonService;
import com.zcsy.manage.service.news.ICellManualService;
import com.zcsy.manage.service.news.INoticeInfoService;

/**
 * @Title: CellManualController.java
 * @Package： com.zcsy.manage.web.news 
 * @Description: 社区政务控制层管理类
 * @author liuying 1075817474@qq.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2016年8月2日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
@RequestMapping(value = "/cellManual")
public class CellManualController {

	@Autowired
	private ICellManualService cellManualService ;

	@Autowired
	private INoticeInfoService noticeInfoService;

	/**
	 * @Description 社区政务列表显示
	 * @param pNo 起始页
	 * @return 跳转页面
	 * @author liuying 1075817474@qq.com
	 * @date 2016年8月2日
	 * @see 
	 */
	@RequestMapping(value="cellManualList" , method = RequestMethod.GET)
    public String cellManualList(Model model, Integer pNo, HttpServletRequest req){

        String cellId = ((CellInfo) req.getSession().getAttribute(RequestConstants.OWNER_CELL_INFO)).getId();
		User user = CommonService.getSessionUser();
						
		if(pNo == null){
            pNo = Constants.PAGE_NO;
        }
		
        //此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
        PageHelper.startPage(pNo, Constants.PAGE_SIZE);
        
        CellManual cellManual = new CellManual();
        cellManual.setCellId(cellId);
        
        List<CellManual> cellManuals = cellManualService.selectByKeyword(cellManual);
        PageInfo<CellManual> info = new PageInfo<CellManual>(cellManuals);

		//未读条数查询
        int unRead = noticeInfoService.selectUnreadCount(user.getId(), cellId);
		
		model.addAttribute("cellManuals", info);
		model.addAttribute("currentPage", pNo);
		model.addAttribute("unRead", unRead);
		
        return "news/cellmanual/cellManualList";
	}
	
	/**
	 * @Description 社区政务的详情展示
	 * @param id 主键id
	 * @return 跳转页面
	 * @author liuying 1075817474@qq.com
	 * @date 2016年8月2日
	 * @see 
	 */
	@RequestMapping(value = "/cellManualDetail", method = RequestMethod.GET)
    public String cellManualDetail(Model model, String id, HttpServletRequest req){
		
	    try {
	    	CellManual cellManual = cellManualService.selectByPrimaryKey(id);
	        if(cellManual != null){
	            model.addAttribute("cellManual", cellManual);
	        }
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
        return "news/cellmanual/cellManualDetail";
    }
	
	/**
	 * @Description:展示社区政务的上一篇和下一篇
	 * @param flag 标记是展示上一篇和下一篇(0:上一篇；1:下一篇)
	 * @param id 当前主键id
	 * @return 页面跳转
	 * @author huangls 1449700087@qq.com
	 * @date 2016年8月15日
	 */
	@RequestMapping(value = "lastOrNextOne", method = RequestMethod.GET)
	public String lastOrNextOne(Model model, Integer flag, String id, HttpServletRequest req) {
		
        String cellId = ((CellInfo) req.getSession().getAttribute(RequestConstants.OWNER_CELL_INFO)).getId();
//		User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
		CellManual cellManual = new CellManual();

    	List<String> ids = cellManualService.selectIdList(cellId);
    	for(int i=0; i<ids.size(); i++){
    		if(id.equals(ids.get(i))){
    			//当需要展示上一篇且有上一篇时
    			if(flag == 0 && i >0){
    				id = ids.get(i-1);
    				break;
    			}
    			//当需要展示下一篇且有下一篇时
    			if(flag == 1 && i <ids.size()-1){
    				id = ids.get(i+1);
    				break;
    			}
    		}
    	}
		
		
    	
    	cellManual = cellManualService.selectByPrimaryKey(id);
	    model.addAttribute("cellManual", cellManual);
	    
        return "news/cellmanual/cellManualDetail";
    }
	
}
